/*
 * Created by JFormDesigner on Thu May 27 19:08:41 CST 2021
 */

package renthouse.view.information.uesr;

import renthouse.dao.HouseDao;
import renthouse.dao.OrderDao;
import renthouse.entry.House;
import renthouse.entry.Order;
import renthouse.util.StringUtil;
import renthouse.view.sever.LoginFrame;

import java.awt.*;
import javax.swing.*;

/**
 * @author Brainrain
 */
public class UserAddOrder extends JFrame {

    private int MasterIdVal;
    private HouseDao houseDao = new HouseDao();//为什么要私有化
    private int houseId;
    GridBagLayout gridBagLayout=new GridBagLayout();

    public UserAddOrder(int houseId) throws HeadlessException {
        this.houseId = houseId;
        initComponents();
        House house = houseDao.queryById(houseId);
        HouseId.setText(String.valueOf(houseId));
        HouseAdress.setText(house.getAddress());
        HouseState.setSelectedItem(house.getState());
        HouseIdStyle.setText(house.getStyle());
        HouseIdRent.setText(String.valueOf(Integer.valueOf(house.getRent())));
        MasterIdVal = house.getMasterid();
    }

    private void button1ActionPerformed() {
        // TODO add your code here
        String houseIdVal = HouseId.getText();
        String start = HouseIdStart.getText();
        String end = HouseIdEnd.getText();
        String addressVal = HouseAdress.getText();
        String stateVal = "已出租";
        String styleVal = HouseIdStyle.getText();
        String rentpriceVal = HouseIdRent.getText();


        if (StringUtil.isNull(addressVal)) {
            JOptionPane.showMessageDialog(null, "地址不能为空");
            return;
        }

        if (StringUtil.isNull(stateVal)) {
            JOptionPane.showMessageDialog(null, "状态不能为空");
            return;
        }

        if (StringUtil.isNull(styleVal)) {
            JOptionPane.showMessageDialog(null, "风格不能为空");
            return;
        }
        if (StringUtil.isNull(rentpriceVal)) {
            JOptionPane.showMessageDialog(null, "租金不能为空");
            return;
        }
        if (StringUtil.isNull(houseIdVal)) {
            JOptionPane.showMessageDialog(null, "房屋Id不能为空");
            return;
        }
        if (StringUtil.isNull(start)) {
            JOptionPane.showMessageDialog(null, "开始租房时间不能为空");
            return;
        }
        if (StringUtil.isNull(end)) {
            JOptionPane.showMessageDialog(null, "结束租房时间不能为空");
            return;
        }

        Order order = new Order();
        OrderDao orderDao = new OrderDao();
        order.setMasterid(MasterIdVal);
        order.setUserid(LoginFrame.loginuser.getId());
        order.setAddress(addressVal);
        order.setState(stateVal);
        order.setStyle(styleVal);
        order.setRent(Integer.valueOf(rentpriceVal));
        order.setStart(start);
        order.setEnd(end);

        boolean f = orderDao.insert(order);

        if (f) {
            JOptionPane.showMessageDialog(null, "下单成功");

        } else {
            JOptionPane.showMessageDialog(null, "下单失败");
        }

        setVisible(false);
        UserFindOrder userFindOrder = new UserFindOrder();
        userFindOrder.setVisible(true);


    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        image = new JLabel();
        label1 = new JLabel();
        label3 = new JLabel();
        label4 = new JLabel();
        label5 = new JLabel();
        label6 = new JLabel();
        label7 = new JLabel();
        label8 = new JLabel();
        HouseId = new JTextField();
        HouseAdress = new JTextField();
        HouseIdStyle = new JTextField();
        HouseIdRent = new JTextField();
        HouseIdStart = new JTextField();
        HouseIdEnd = new JTextField();
        button1 = new JButton();
        button2 = new JButton();
        HouseState = new JComboBox<>();

        //======== this ========
        setTitle("\u623f\u5ba2\u4e0b\u5355\u754c\u9762");
        Container contentPane = getContentPane();
        contentPane.setLayout(null);
        contentPane.add(image);
        image.setBounds(35, 30, 90, 50);

        //---- label1 ----
        label1.setText("\u623f\u5c4bID");
        contentPane.add(label1);
        label1.setBounds(200, 55, 50, label1.getPreferredSize().height);

        //---- label3 ----
        label3.setText("\u623f\u5c4b\u5730\u5740");
        contentPane.add(label3);
        label3.setBounds(30, 95, 50, 17);

        //---- label4 ----
        label4.setText("\u623f\u5c4b\u72b6\u6001");
        contentPane.add(label4);
        label4.setBounds(200, 95, 50, 17);

        //---- label5 ----
        label5.setText("\u623f\u5c4b\u98ce\u683c");
        contentPane.add(label5);
        label5.setBounds(30, 130, 50, 17);

        //---- label6 ----
        label6.setText("\u6bcf\u6708\u79df\u91d1");
        contentPane.add(label6);
        label6.setBounds(200, 135, 50, 17);

        //---- label7 ----
        label7.setText("\u5f00\u59cb\u79df\u623f\u65f6\u95f4");
        contentPane.add(label7);
        label7.setBounds(25, 170, 80, 17);

        //---- label8 ----
        label8.setText("\u7ed3\u675f\u79df\u623f\u65f6\u95f4");
        contentPane.add(label8);
        label8.setBounds(200, 170, 65, 17);
        contentPane.add(HouseId);
        HouseId.setBounds(280, 55, 80, HouseId.getPreferredSize().height);
        contentPane.add(HouseAdress);
        HouseAdress.setBounds(100, 90, 75, 24);
        contentPane.add(HouseIdStyle);
        HouseIdStyle.setBounds(100, 130, 75, 24);
        contentPane.add(HouseIdRent);
        HouseIdRent.setBounds(280, 130, 80, 24);
        contentPane.add(HouseIdStart);
        HouseIdStart.setBounds(105, 165, 70, 24);
        contentPane.add(HouseIdEnd);
        HouseIdEnd.setBounds(280, 165, 80, 24);

        //---- button1 ----
        button1.setText("\u786e\u5b9a\u4e0b\u5355");
        button1.addActionListener(e -> button1ActionPerformed());
        contentPane.add(button1);
        button1.setBounds(65, 230, 80, button1.getPreferredSize().height);

        //---- button2 ----
        button2.setText("\u53d6\u6d88\u4e0b\u5355");
        contentPane.add(button2);
        button2.setBounds(225, 230, 80, 24);

        //---- HouseState ----
        HouseState.setModel(new DefaultComboBoxModel<>(new String[] {
            "\u5df2\u51fa\u79df",
            "\u5f85\u51fa\u79df"
        }));
        contentPane.add(HouseState);
        HouseState.setBounds(280, 90, 75, HouseState.getPreferredSize().height);

        { // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JLabel image;
    private JLabel label1;
    private JLabel label3;
    private JLabel label4;
    private JLabel label5;
    private JLabel label6;
    private JLabel label7;
    private JLabel label8;
    private JTextField HouseId;
    private JTextField HouseAdress;
    private JTextField HouseIdStyle;
    private JTextField HouseIdRent;
    private JTextField HouseIdStart;
    private JTextField HouseIdEnd;
    private JButton button1;
    private JButton button2;
    private JComboBox<String> HouseState;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
